import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },

  {
    path: '/example',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    meta: { title: 'Example', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'table',
        name: 'Table',
        component: () => import('@/views/table/index'),
        meta: { title: 'Table', icon: 'table' }
      },
      {
        path: 'tree',
        name: 'Tree',
        component: () => import('@/views/tree/index'),
        meta: { title: 'Tree', icon: 'tree' }
      }
    ]
  },

  {
    path: '/form',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Form',
        component: () => import('@/views/form/index'),
        meta: { title: 'Form', icon: 'form' }
      }
    ]
  },

  {
    component: Layout,
    redirect: '/nested/menu1',
    path: '/进货管理',
    name: '进货管理',
    meta: { title: '进货管理' },
    children: [
      {
        path: '进货入库',
        component: () => import('@/views/nested/menu1/menu1-1/index.vue'),
        name: '进货入库',
        meta: { title: '进货入库' }
      },
      {
        path: '退货出库',
        component: () => import('@/views/nested/menu1/menu1-2/ReturnGoods.vue'),
        name: '退货出库',
        meta: { title: '退货出库' },
      },
      {
        path: '进货单据查询',
        component: () => import('@/views/nested/menu1/menu1-3/index.vue'),
        name: '进货单据查询',
        meta: { title: '进货单据查询' }
      },
      {
        path: '退货单据查询',
        component: () => import('@/views/nested/menu1/menu1-4/index.vue'),
        name: '退货单据查询',
        meta: { title: '退货单据查询' }
      },
      {
        path: '当前库存查询',
        component: () => import('@/views/nested/menu1/menu1-5/index.vue'),
        name: '当前库存查询',
        meta: { title: '当前库存查询' }
      }
    ]
  },

  {
    component: Layout,
    redirect: '/nested/menu2',
    path: '/销售管理',
    name: '销售管理',
    meta: { title: '销售管理' },
    children: [
      {
        path: '销售出库',
        component: () => import('@/views/nested/menu2/menu2-1/index.vue'),
        name: '销售出库',
        meta: { title: '销售出库' }
      },
      {
        path: '客户退货',
        component: () => import('@/views/nested/menu2/menu2-2/index.vue'),
        name: '客户退货',
        meta: { title: '客户退货' },
      },
      {
        path: '销售单据查询',
        component: () => import('@/views/nested/menu2/menu2-3/index.vue'),
        name: '销售单据查询',
        meta: { title: '销售单据查询' }
      },
      {
        path: '客户退货查询',
        component: () => import('@/views/nested/menu2/menu2-4/index.vue'),
        name: '客户退货查询',
        meta: { title: '客户退货查询' }
      },
      {
        path: '当前库存查询',
        component: () => import('@/views/nested/menu2/menu2-5/index.vue'),
        name: '当前库存查询',
        meta: { title: '当前库存查询' }
      }
    ]
  },

  {
    component: Layout,
    redirect: '/nested/menu3',
    path: '/库存管理',

    name: '库存管理',
    meta: { title: '库存管理' },
    children: [
      {
        path: '商品报损',
        component: () => import('@/views/nested/menu3/menu3-1/index.vue'),
        name: '商品报损',
        meta: { title: '商品报损' }
      },
      {
        path: '商品报溢',
        component: () => import('@/views/nested/menu3/menu3-2/index.vue'),
        name: '商品报溢',
        meta: { title: '商品报溢' },
      },
      {
        path: '库存报警',
        component: () => import('@/views/nested/menu3/menu3-3/index.vue'),
        name: '库存报警',
        meta: { title: '库存报警' }
      },
      {
        path: '报损报溢查询',
        component: () => import('@/views/nested/menu3/menu3-4/index.vue'),
        name: '报损报溢查询',
        meta: { title: '报损报溢查询' }
      },
      {
        path: '当前库存查询',
        component: () => import('@/views/nested/menu3/menu3-5/index.vue'),
        name: '当前库存查询',
        meta: { title: '当前库存查询' }
      }
    ]
  },

  {
    component: Layout,
    redirect: '/nested/menu4',
    path: '/统计报表',
    name: '统计报表',
    meta: { title: '统计报表' },
    children: [
      {
        path: '供应商统计',
        component: () => import('@/views/nested/menu4/menu4-1/index.vue'),
        name: '供应商统计',
        meta: { title: '供应商统计' }
      },
      {
        path: '客户统计',
        component: () => import('@/views/nested/menu4/menu4-2/index.vue'),
        name: '客户统计',
        meta: { title: '客户统计' },
      },
      {
        path: '商品采购统计',
        component: () => import('@/views/nested/menu4/menu4-3/index.vue'),
        name: '商品采购统计',
        meta: { title: '商品采购统计' }
      },
      {
        path: '商品销售统计',
        component: () => import('@/views/nested/menu4/menu4-4/index.vue'),
        name: '商品销售统计',
        meta: { title: '商品销售统计' }
      },
      {
        path: '按日统计分析',
        component: () => import('@/views/nested/menu4/menu4-5/index.vue'),
        name: '按日统计分析',
        meta: { title: '按日统计分析' }
      },
      {
        path: '按月统计分析',
        component: () => import('@/views/nested/menu4/menu4-6/index.vue'),
        name: '按月统计分析',
        meta: { title: '按月统计分析' }
      }
    ]
  },

  {
    path: '/基础资料',
    component: Layout,
    redirect: '/nested/menu5',
    name: '基础资料',
    meta: { title: '基础资料' },
    children: [
      {
        path: '供应商管理',
        component: () => import('@/views/nested/menu5/menu5-1/index.vue'),
        name: '供应商管理',
        meta: { title: '供应商管理' }
      },
      {
        path: '客户管理',
        component: () => import('@/views/nested/menu5/menu5-2/index.vue'),
        name: '客户管理',
        meta: { title: '客户管理' },
      },
      {
        path: '商品管理',
        component: () => import('@/views/nested/menu5/menu5-3/index.vue'),
        name: '商品管理',
        meta: { title: '商品管理' }
      },
      {
        path: '初期库存',
        component: () => import('@/views/nested/menu5/menu5-4/index.vue'),
        name: '初期库存',
        meta: { title: '初期库存' }
      }
    ]
  },

  {
    path: '/系统管理',
    component: Layout,
    redirect: '/nested/menu6',
    name: '系统管理',
    meta: { title: '系统管理' },
    children: [
      {
        path: '角色管理',
        component: () => import('@/views/nested/menu6/menu6-1/index.vue'),
        name: '角色管理',
        meta: { title: '角色管理' }
      },
      {
        path: '用户管理',
        component: () => import('@/views/nested/menu6/menu6-2/index.vue'),
        name: '用户管理',
        meta: { title: '用户管理' },
      },
      {
        path: '系统日志',
        component: () => import('@/views/nested/menu6/menu6-3/index.vue'),
        name: '系统日志',
        meta: { title: '系统日志' }
      },
      {
        path: '修改密码',
        component: () => import('@/views/nested/menu6/menu6-4/index.vue'),
        name: '修改密码',
        meta: { title: '修改密码' }
      },
      {
        path: '安全退出',
        component: () => import('@/views/nested/menu6/menu6-5/index.vue'),
        name: '安全退出',
        meta: { title: '安全退出' }
      }
    ]
  },

  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: '外链', icon: 'link' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
